
#include <QListWidget>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QSortFilterProxyModel>
#include <QButtonGroup>
#include <QPushButton>
#include "EditSymbolView.h"
#include "SymbolModel.h"

#include "SymbolView.h"

SymbolView::SymbolView( QWidget * parent ) : QWidget( parent )
{
    listView = new QListView();
    
    QHBoxLayout * main_layout = new QHBoxLayout( this );
    
    filteredModel = new QSortFilterProxyModel( this );
    listView->setModel( filteredModel );
    editSymbolView = new EditSymbolView();
    
    main_layout->addLayout( createSymbolList() );
    main_layout->addWidget( editSymbolView );
    
    connect( searchLineEdit, SIGNAL( textChanged(QString) ), filteredModel, SLOT( setFilterWildcard(QString) ) );
    connect( listView->selectionModel(), SIGNAL( currentChanged( QModelIndex, QModelIndex ) ),
        this, SLOT( currentChanged( QModelIndex, QModelIndex ) ) );
}

void SymbolView::setSymbolModel( SymbolModel * model )
{
    symbolModel = model;
    filteredModel->setSourceModel( model );
    editSymbolView->setModel( model );
}

void SymbolView::setCurrent( const QModelIndex & index )
{
    listView->setCurrentIndex( filteredModel->mapFromSource( index ) );
}

void SymbolView::buttonClicked( int buttonIndex )
{
    switch( buttonIndex )
    {
        case ADD_BUTTON:
        {
            int 
                selected_row;
            QModelIndexList
                selected_indexes;
                
            selected_indexes = listView->selectionModel()->selectedIndexes();
                
            if( selected_indexes.size() )
            {
                Q_ASSERT( selected_indexes.size() == 1 );
                selected_row = selected_indexes[ 0 ].row();
            }
            else
            {
                selected_row = 0;
            }

            symbolModel->insertRow( selected_row );
        }
        break;
        
        case DELETE_BUTTON:
        {
            QModelIndexList
                selected_indexes;
                
            selected_indexes = listView->selectionModel()->selectedIndexes();
                
            if( selected_indexes.size() )
            {
                Q_ASSERT( selected_indexes.size() == 1 );
                
                //languageMod->removeRow( selected_indexes[ 0 ] );
                symbolModel->removeRow( selected_indexes[ 0 ].row() );
            }

        }
        break;
        
        default:
            break;
    }
}

QLayout * SymbolView::createSymbolList()
{
    QVBoxLayout * list_layout = new QVBoxLayout();
    
    searchLineEdit = new QLineEdit;
    
    addSymbolButton = new QPushButton( QIcon(":/images/edit_add.png"), "" );
    deleteSymbolButton = new QPushButton( QIcon(":/images/edit_remove.png"), "" );
    listEditButtonGroup = new QButtonGroup;
    
    listEditButtonGroup->addButton( addSymbolButton, ADD_BUTTON );
    listEditButtonGroup->addButton( deleteSymbolButton, DELETE_BUTTON );
    
    QHBoxLayout * list_edit_button_layout = new QHBoxLayout();
    list_edit_button_layout->addWidget( addSymbolButton );
    list_edit_button_layout->addWidget( deleteSymbolButton );
    
    list_layout->addWidget( searchLineEdit );
    list_layout->addWidget( listView );
    list_layout->addLayout( list_edit_button_layout );
    
    connect( listEditButtonGroup, SIGNAL( buttonClicked(int) ), this, SLOT( buttonClicked(int) ) );
    
    
    return list_layout;
}

void SymbolView::currentChanged( const QModelIndex & current, const QModelIndex & previous )
{
    editSymbolView->currentChanged( 
        filteredModel->mapToSource( current ),
        filteredModel->mapToSource( previous )
        );
        
    emit symbolIndexChanged( filteredModel->mapToSource( current ) );
}